package com.yeyks.controller.admin;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.appcommon.dto.AppFileResult;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.controller.BaseController;
import com.yeyks.service.AppFileService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = "AppFileController_文件上传模块", description = "文件上传模块的相关接口信息")
@RestController
@RequestMapping("/admin/file")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppFileController extends BaseController {

    private final AppFileService appFileService;

    /**
     * 单文件的上传
     *
     * @param token
     * @param rdName
     * @param pubArea
     * @param file
     * @return
     */
    @ApiOperation("单文件的上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "有效令牌", required = true, paramType = "query",dataType = "String")
    })
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonResult<AppFileResult> uploadFile(
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "platform", required = false) String platform,
            @RequestParam(name = "rdName", defaultValue = "", required = true) Boolean rdName,
            @RequestParam(name = "pubArea", defaultValue = "", required = true) Boolean pubArea,
            @RequestParam(name = "file") MultipartFile file
    ) {

        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return appFileService.uploadFile(rdName, pubArea, file, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    /**
     * 多文件的上传
     *
     * @param token
     * @param rdName
     * @param pubArea
     * @return
     */
    @ApiOperation("多文件的上传")
    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    public CommonResult<List<AppFileResult>> uploadFiles(
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "platform", required = false) String platform,
            @RequestParam(name = "rdName", defaultValue = "", required = true) Boolean rdName,
            @RequestParam(name = "pubArea", defaultValue = "", required = true) Boolean pubArea,
            @RequestParam(name = "files", defaultValue = "", required = true) MultipartFile[] files
    ) {

        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return appFileService.uploadFiles(rdName, pubArea, Arrays.asList(files),
                MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("删除单个文件")
    @PostMapping("/delete")
    public CommonResult deleteFile(@RequestParam(name = "token", defaultValue = "", required = true) String token,
                                   @RequestParam(value = "version", required = false) String version,
                                   @RequestParam(value = "platform", required = false) String platform,
                                   @ApiParam(required = true, name = "absOssKey", value = "删除单个文件的Key")
                                   @RequestParam(name = "absOssKey") String absOssKey){

        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return appFileService.deleteFile(absOssKey, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }
}
